package com.iwhalecloud.bss.kite.cucc.service.scene;

import com.iwhalecloud.bss.kite.client.api.scene.query.IQuerySceneAccessProdInstService;
import com.iwhalecloud.bss.kite.client.dto.inst.InstAttrDTO;
import com.iwhalecloud.bss.kite.common.constant.KeyConsts;
import com.iwhalecloud.bss.kite.common.constant.SymbolConsts;
import com.iwhalecloud.bss.kite.common.util.KiteBeanUtils;
import com.iwhalecloud.bss.kite.common.util.KiteListUtils;
import com.iwhalecloud.bss.kite.common.util.KiteStringUtils;
import com.iwhalecloud.bss.kite.cucc.client.api.scene.ILocalProdAttrService;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.CuccProdAttrVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.FieldNodeDataVO;
import com.iwhalecloud.bss.kite.cucc.client.vo.scene.LinkedProdConditionVO;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalKeyConsts;
import com.iwhalecloud.bss.kite.cucc.common.constant.LocalServiceOfferConsts;
import com.iwhalecloud.bss.kite.dataservice.cache.DcPublicCache;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttrValue;
import com.iwhalecloud.bss.kite.manager.inst.KiteSceneInst;
import com.iwhalecloud.bss.kite.order.client.context.OrderContext;
import com.ztesoft.bss.base.entity.AttrValue;
import com.iwhalecloud.bss.kite.dataservice.entity.DcPublic;
import com.iwhalecloud.bss.kite.manager.api.IQuerySceneInstService;
import com.iwhalecloud.bss.kite.manager.inst.KiteAttr;
import com.iwhalecloud.bss.kite.manager.inst.KiteProdInst;
import com.ztesoft.bss.base.staticdata.ServiceOfferConst;
import com.ztesoft.bss.common.util.ContextUtil;
import com.ztesoft.bss.svcability.inf.IKeyValues;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;
import java.util.ArrayList;
import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * @Author: Zhu
 * @Date: 2020/5/14
 */
@Service
public class LocalProdAttrService implements ILocalProdAttrService {

    @Autowired
    private IQuerySceneAccessProdInstService querySceneProdInstService;

    @Autowired
    private IQuerySceneInstService querySceneInstService;

    public static String DC_STYPE = "20220225001";

    @Override
    public List<CuccProdAttrVO> listCuccProdInstAttrs(String sceneInstId, String custId, String prodInstId, String prodRoleId) {
        List<CuccProdAttrVO> prodAttrList = new ArrayList<>();
        List<InstAttrDTO> kiteAttrList = querySceneProdInstService
            .listSceneProdInstAttrByProdInstId(sceneInstId, prodInstId, custId).getResultObject();
        kiteAttrList.forEach(kiteAttr -> prodAttrList.add(KiteBeanUtils
            .transform(kiteAttr, CuccProdAttrVO.class)));
        //属性分组
        List<DcPublic> dcPublicList = DcPublicCache.get(LocalKeyConsts.GROUP_ATTR).stream().filter(dcPublic -> {
            return dcPublic.getPkey().equals(prodRoleId);
        }).collect(Collectors.toList());
        if (KiteListUtils.isNotEmpty(dcPublicList)) {
            prodAttrList.forEach(prodAttrVO -> {
                dcPublicList.forEach(dcPublic -> {
                    if (dcPublic.getCodea().toString().contains(prodAttrVO.getFieldName())) {
                        prodAttrVO.setGroupName(dcPublic.getPname());
                        prodAttrVO.setGroupId(dcPublic.getPcode());
                    }
                });

            });
        }
        return prodAttrList;


    }

    @Override
    public LinkedProdConditionVO listLinkedProdConditionAttrs(String sceneInstId, String prodId, String groupId) {
        String provinceCode = ContextUtil.getLoginInfo().getUserInfo().getPostProvinceNbr();
        List<DcPublic> dcPublicList = DcPublicCache.getByPcode(DC_STYPE, prodId);
        // codea同时支持多省份或地市配置，中间用","隔开。如"12/13/10"
        List<DcPublic> dcPublicLists = dcPublicList.stream().filter(dcPublic -> KiteStringUtils.isIndexOf(dcPublic.getCodea(), provinceCode, SymbolConsts.SLASH)).collect(Collectors.toList());
        List<KiteProdInst> kiteProdInsts = querySceneInstService.listProdInsts(sceneInstId);
        KiteProdInst prodInst = kiteProdInsts.stream().filter(kiteProdInst -> KiteStringUtils.equals(kiteProdInst.getProdId(), prodId)).findFirst().orElse(null);
        LinkedProdConditionVO linkedProdConditionVO = new LinkedProdConditionVO();
        List<FieldNodeDataVO> fieldNodeDataVOList = new ArrayList<>();
        linkedProdConditionVO.setFieldNodeDataList(fieldNodeDataVOList);
        linkedProdConditionVO.setProdId(prodId);
        if (ObjectUtils.isEmpty(prodInst) || KiteListUtils.isEmpty(dcPublicLists)) {
            return linkedProdConditionVO;
        }

        String serviceOfferId = querySceneInstService.getSceneInst(sceneInstId).getServiceOfferId();

        List<KiteAttr> kiteAttrList = prodInst.listAttrs();
        assembProdConditionVO(kiteAttrList, dcPublicLists, fieldNodeDataVOList, groupId, serviceOfferId);
        return linkedProdConditionVO;
    }


    public void assembProdConditionVO(List<KiteAttr> kiteAttrList, List<DcPublic> dcPublicList, List<FieldNodeDataVO> fieldNodeDataVOList, String groupId, String serviceOfferId) {

        //资源核查不用返回
        if (KiteStringUtils.equals(serviceOfferId, LocalServiceOfferConsts.SERVICE_OFFER_RESOURCE_CHECK)) {
           return;
        }
        kiteAttrList.forEach(attr -> {
            dcPublicList.forEach(dcPublic -> {
                if (KiteStringUtils.equalsAny(dcPublic.getCoded(), groupId, "-1") && (KiteStringUtils.equals(dcPublic.getCodeb(), attr.getFieldName()))) {
                    FieldNodeDataVO data = new FieldNodeDataVO();
                    data.setFieldCode(attr.getAttrId());
                    data.setFieldName(attr.getFieldName());
                    data.setAttrName(attr.getCname());
                    data.setRequried(KiteStringUtils.equals(dcPublic.getCodec(), IKeyValues.IFTRUE_T) ? true : false);
                    data.setValueTypeId(attr.getAttrValueTypeId());
                    data.setValue(attr.getValue());
                    if (KiteStringUtils.equals(serviceOfferId, ServiceOfferConst.service_offer_2826)) {
                        data.setIsEdit(LocalKeyConsts.IFTRUE_F);
                    }
                    else {
                        data.setIsEdit(attr.getIsEdit());
                    }

                    data.setDefValue("");
                    if (KiteStringUtils.isEmpty(attr.getValue()) && KiteStringUtils.isNotEmpty(dcPublic.getCodee())) {
                        data.setDefValue(dcPublic.getCodee());
                    }

                    // 如果是下拉框， 需要把下拉框选项塞进去

                    List<AttrValue> attrValues = new ArrayList<AttrValue>();
                    if (KiteStringUtils.equals(attr.getAttrValueTypeId(), KeyConsts.DROPDOWN)) {
                        List<KiteAttrValue> attrAttrValues = attr.getAttrValues();
                        attrAttrValues.forEach(attrValue -> {
                            AttrValue value = new AttrValue();
                            value.setAttrId(Long.valueOf(attrValue.getAttrId()));
                            value.setAttrValue(attrValue.getAttrValue());
                            value.setAttrValueDesc(attrValue.getAttrValueName());
                            value.setAttrValueName(attrValue.getAttrValueName());
                            attrValues.add(value);
                        });
                    }
                    data.setAttrValueList(attrValues);
                    fieldNodeDataVOList.add(data);
                }
            });

        });

    }

}
